<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>课程管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-select v-model="select_status" placeholder="筛选状态" class="handle-select mr10" @change="selectUserStatus">
          <el-option key="1" label="启用" value="0"></el-option>
            <el-option key="2" label="禁用" value="1"></el-option>
              <el-option key="3" label="全部" value="-1"></el-option>
        </el-select>
        <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="addVisible = true">添加课程</el-button>
      </div>
      <el-table
        :data="data.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="80" align="center" sortable label="序列"></el-table-column>
        <el-table-column prop="cname" label="课程名称" width="180"></el-table-column>
        <el-table-column prop label="课程海报" width="100">
          <template slot-scope="scope">
            <el-image :src="`http://localhost/fitness/public/static/img/`+scope.row.cpic"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="cintro" label="课程简介" width="auto"></el-table-column>
        <el-table-column prop="cdate" label="授课日期" sortable width="200"></el-table-column>
        <el-table-column prop="coachname" label="授课教练" width="100"></el-table-column>
        <el-table-column prop="level" label="等级" width="80"></el-table-column>
        <el-table-column prop="cadress" label="授课地址" width="200"></el-table-column>
        <el-table-column prop="price" label="课程价格" width="80"></el-table-column>
        <el-table-column prop="cstatus" label="课程状态" width="100" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.cstatus===0">
              <i class="el-icon-circle-check" style="color:#1e90ff">已启用</i>
            </template>
            <template v-else>
              <i class="el-icon-circle-close" style="color:red">已禁用</i>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.cstatus===0">
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleBan(scope.row)"
              >禁用</el-button>
            </template>
            <template v-else>
              <el-button
                type="text"
                icon="el-icon-circle-check"
                @click="handleOnCouse(scope.row)"
              >启用</el-button>
              <el-button type="text" icon="el-icon-edit" @click="onclickEdit(scope.row)">修改</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 8, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="courseData.length"
        ></el-pagination>
      </div>
    </div>

    <!-- 添加课程弹出框 -->
    <el-dialog title="请填写课程信息" :visible.sync="addVisible" width="30%">
      <el-form ref="addform" :model="addform" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="cname">
          <el-input v-model="addform.cname"></el-input>
        </el-form-item>
        <el-form-item label="授课教练" prop="coachid">
          <el-select v-model="addform.coachid" placeholder="请选择">
            <el-option
              v-for="item in coachData"
              :key="item.coachid"
              :label="item.coachname"
              :value="item.coachid"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="课程海报" ref="cpic">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Course/addcouse"
            name="pic"
            :file-list="fileList"
            :limit="1"
            list-type="picture"
            :on-success="handlePicsuccess"
            accept=".jpg, .png"
            :auto-upload="false"
            :data="addform"
            :on-change="handleChange"
            :on-remove="handleRemove"
          >
            <el-button size="small" type="primary" @click="piclimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">图片不能为空，只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="授课地点" prop="cadress">
          <el-input v-model="addform.cadress"></el-input>
        </el-form-item>
        <el-form-item label="课程简介" prop="cintro">
          <el-input type="textarea" v-model="addform.cintro"></el-input>
        </el-form-item>
        <el-form-item label="授课时间" prop="cdate">
          <el-date-picker v-model="addform.cdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="课程价格" prop="price">
          <el-input v-model="addform.price"></el-input>
        </el-form-item>
        <el-form-item label="课程等级" prop="level">
          <el-input v-model="addform.level"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹出框 -->
    <el-dialog title="修改课程信息" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="课程名称" prop="cname">
          <el-input v-model="form.cname"></el-input>
        </el-form-item>
        <el-form-item label="授课教练" prop="coachid">
          <el-select v-model="form.coachid" placeholder="请重新选择教练" @change="onClickChange">
            <el-option
              v-for="item in coachData"
              :key="item.coachid"
              :label="item.coachname"
              :value="item.coachid"
            ></el-option>
          </el-select>
          <!-- <p>原教练:{{form.coachname}}</p> -->
        </el-form-item>
        <el-form-item label="课程海报">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Course/editcouse"
            name="pic"
            :file-list="fileListEdit"
            :limit="1"
            list-type="picture"
            accept=".jpg, .png"
            :before-upload="beforeAvatarUpload"
            :on-success="handlePicsuccess"
            :auto-upload="false"
            :data="form"
          >
            <el-button size="small" type="primary" @click="piclimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="授课地点" prop="cadress">
          <el-input v-model="form.cadress"></el-input>
        </el-form-item>
        <el-form-item label="授课时间" prop="cdate">
          <el-date-picker v-model="form.cdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="课程价格" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="课程等级" prop="level">
          <el-input v-model="form.level"></el-input>
        </el-form-item>
        <el-form-item label="课程简介" prop="cintro">
          <el-input v-model="form.cintro"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 禁用提示框 -->
    <el-dialog title="提示" :visible.sync="banVisible" width="300px" center>
      <div class="del-dialog-cnt">确定禁用该课程？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="banVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmBan">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 启用提示框 -->
    <el-dialog title="提示" :visible.sync="onVisible" width="300px" center>
      <div class="del-dialog-cnt">确定启用该课程？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmOn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { log } from "util";
export default {
  name: "basetable",
  data() {
    return {
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      // userList: [],
      select_status: '全部',
      multipleSelection: [],
      select_cate: "",
      select_word: "",
      is_search: false,
      addVisible: false,
      editVisible: false,
      banVisible: false,
      onVisible: false,
      courseData: [],
      coachData: [],
      fileList: [],
      fileListEdit: [],
      addform: {
        cname: "",
        cdate: "",
        price: "",
        level: "",
        cintro: "",
        cadress: "",
        cpic: "",
        coachid: ""
      },
      form: {
        cname: "",
        cdate: "",
        price: "",
        level: "",
        cintro: "",
        cadress: "",
        coachid: ""
      },
      rules: {
        cname: [
          {
            required: true,
            message: "课程名称不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5]+$/,
            message: "课程只能为中文",
            trigger: "blur"
          }
        ],
        coachid: [
          {
            required: true,
            message: "请选择教练",
            trigger: "change"
          }
        ],
        cadress: [
          {
            required: true,
            message: "请填写授课地址",
            trigger: "blur"
          }
        ],
        cintro: [
          {
            required: true,
            message: "课程简介不能为空",
            trigger: "blur"
          }
        ],
        cdate: [
          {
            type: "string",
            required: true,
            message: "请选择授课时间",
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: "课程价格不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[0-9]*$/,
            message: "课程价格只能是数字",
            trigger: "blur"
          }
        ],
        level: [
          {
            required: true,
            message: "课程等级不能为空",
            trigger: "blur"
          }
        ]
      },
      rules1: {
        cname: [
          {
            required: true,
            message: "课程名称不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5]+$/,
            message: "课程只能为中文",
            trigger: "blur"
          }
        ],
        coachid: [
          {
            required: true,
            message: "请选择教练",
            trigger: "change"
          }
        ],
        cadress: [
          {
            required: true,
            message: "请填写授课地址",
            trigger: "blur"
          }
        ],
        cintro: [
          {
            required: true,
            message: "课程简介不能为空",
            trigger: "blur"
          }
        ],
        cdate: [
          {
            type: "string",
            required: true,
            message: "请选择授课时间",
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: "课程价格不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[0-9]*$/,
            message: "课程价格只能是数字",
            trigger: "blur"
          }
        ],
        level: [
          {
            required: true,
            message: "课程等级不能为空",
            trigger: "blur"
          }
        ]
      },
      banCid: 0,
      onCid: 0,
      editVisible: false
    };
  },
  created() {
    this.getData();
    this.getcoach();
  },
  computed: {
    data() {
      return this.courseData.filter(d => {
        if (
          d.cname.indexOf(this.select_word) > -1 ||
          d.cadress.indexOf(this.select_word) > -1
        ) {
          return d;
        }
      });
    }
  },
  methods: {
    selectUserStatus() {
      if (this.select_status == -1) {
        this.getData();
      } else {
        this.getStatusData(this.select_status);
      }
    },
    piclimit(){
      // if (this.fileListEdit && this.fileListEdit.length > 0) {
      //       console.log(this.fileListEdit);
      //       this.$message.error("请删掉第一张图片");
      //     }
      this.$refs.upload.clearFiles();
    },
    handleSizeChange: function(size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
    
    handleRemove(file, fileList) {
      console.log("remove", file, fileList);
      this.fileList = fileList;
    },
    // handlePreview(file) {
    //   console.log(file);
    //   // console.log(this.fileListEdit)
    // },

    beforeAvatarUpload() {
      console.log(this.fileListEdit);
    },
    handlePicsuccess(res, file) {
      console.log("success", res, file);
      console.log(this.fileListEdit);
    },
    handleChange(file, fileList) {
      console.log("777", file, fileList);
      this.fileList = fileList;
    },
    onclickEdit(row) {
      this.editVisible = true;
      console.log("row=", row);
      this.form = JSON.parse(JSON.stringify(row));
      // for(let i=0;i<this.coachData.length;i++){
      //   if(this.coachData[i].coachname==row.coachname){
      //     this.form.coachid=JSON.parse(JSON.stringify(this.coachData[i].coachid));
      //   }
      // }
      console.log("this.form=", this.form);
      this.fileListEdit = [];
      this.fileListEdit.push({
        name: "",
        url: "http://localhost/fitness/public/static/img/" + row.cpic
      });
    },
    getStatusData(status){
        this.$axios.get('/api/Course/courseliststatus',{
        params: {
            status: status,
       }
      }).then(res => {
        this.courseData = res.data;
      })
     },
    getData() {
      this.$axios.get("/api/Course/courselist",{
        params: {
          clubid: localStorage.clubid
        }
      }).then(response => {
        // console.log("response :", response.data);
        this.courseData = response.data;
      });
    },
    getcoach() {
      this.$axios.get("/api/Course/coachlist",{
        params: {
          clubid: localStorage.clubid
        }
      }).then(response => {
        console.log("response :", response.data);
        this.coachData = response.data;
      });
    },
    search() {
      this.is_search = true;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    saveEdit() {
      let vm = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          vm.$axios.post("/api/Course/editcouse", this.form).then(res => {
            console.log("res :", res.data);
          });
          vm.$refs.upload.submit();
          this.$message.success("修改成功");
          this.getData();
          this.editVisible = false;
        } else {
          this.$message.error("修改失败,请填写必填或正确信息");
          this.editVisible = true;
        }
      });
    },
    saveAdd() {
      // console.log(this.addform);
      // this.$axios.post("/api/Course/addcouse", this.addform).then(res => {
      //   console.log("res :", res.data);
      //   if (res.data.flag == 1) {
      //     this.$message.success(`课程添加成功`);
      //     this.getData();
      //   } else {
      //     this.$message.error(`课程添加失败`);
      //   }
      // });
      let vm = this;
      this.$refs["addform"].validate(valid => {
        if (valid) {
          if (!(this.fileList && this.fileList.length > 0)) {
            console.log(this.fileList);
            this.$message.error("添加失败,请添加图片");
            return;
          }
          vm.$refs.upload.submit();
          this.$message.success("添加成功");
          console.log(this.addform);
          this.addVisible = false;
        } else {
          this.$message.error("添加失败,请填写必填信息");
          this.addVisible = true;
        }
      });
    },
    confirmBan() {
      this.$axios
        .get("/api/Course/bancouse", {
          params: {
            cid: this.banCid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.error("禁用成功");
            this.getData();
          }
        });
      this.banVisible = false;
    },
    confirmOn() {
      this.$axios
        .get("/api/Course/oncouse", {
          params: {
            cid: this.onCid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.success("启用成功");
            this.getData();
          }
        });
      this.onVisible = false;
    },
    handleBan(row) {
      this.banVisible = true;
      this.banCid = row.cid;
    },

    handleOnCouse(row) {
      this.onVisible = true;
      console.log(row);
      this.onCid = row.cid;
    },
    editvisible(cid) {
      this.editVisible = true;
      this.form.cid = cid;
    },

    onClickChange(val) {
      console.log("val=", val);
      console.log("form2=", this.form);
    }
  }
};
</script>

<style scoped>
p {
  color: #ff0000;
}
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
